/*
 * This file is part of Cadmium.
 * Copyright (C) 2007-2010 Xavier Clerc.
 *
 * Cadmium is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cadmium is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.x9c.cadmium.primitives.cadmiumjdbc;

import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Clob;

import fr.x9c.cadmium.kernel.Block;
import fr.x9c.cadmium.kernel.Channel;
import fr.x9c.cadmium.kernel.CodeRunner;
import fr.x9c.cadmium.kernel.Custom;
import fr.x9c.cadmium.kernel.Fail;
import fr.x9c.cadmium.kernel.Primitive;
import fr.x9c.cadmium.kernel.PrimitiveProvider;
import fr.x9c.cadmium.kernel.Value;
import fr.x9c.cadmium.primitives.cadmium.Cadmium;

/**
 * This class provides the primitives related to the <tt>java.sql.Clob</tt> class.
 *
 * @author <a href="mailto:cadmium@x9c.fr">Xavier Clerc</a>
 * @version 1.1
 * @since 1.1
 */
@PrimitiveProvider
public final class Clobs {

    /**
     * No instance of this class.
     */
    private Clobs() {
    } // end empty constructor

    /**
     * Frees the passed Clob.
     * @param ctxt context
     * @param clob clob
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_clob_free(final CodeRunner ctxt,
                                              final Value clob)
        throws Fail.Exception {
        try {
            final Clob c = (Clob) clob.asBlock().asCustom();
            c.free();
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_clob_free(CodeRunner, Value)'

    /**
     * Returns an ASCII stream allowing to read the value of the passed Clob.
     * @param ctxt context
     * @param clob clob
     * @return an ASCII stream allowing to read the value of the passed Clob
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_clob_get_ascii_stream(final CodeRunner ctxt,
                                                          final Value clob)
        throws Fail.Exception {
        try {
            final Clob c = (Clob) clob.asBlock().asCustom();
            final InputStream is = c.getAsciiStream();
            final Channel ch = new Channel(is);
            ctxt.getContext().addChannel(ch);
            final Block res = Block.createCustom(Custom.CHANNEL_SIZE,
                                                 Custom.CHANNEL_OPS);
            res.setCustom(ch);
            return Value.createFromBlock(res);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_clob_get_ascii_stream(CodeRunner, Value)'

    /**
     * Returns a substring of the passed Clob.
     * @param ctxt context
     * @param clob clob
     * @param pos starting index of substring
     * @param length length of substring
     * @return a substring of the passed Clob
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_clob_get_sub_string(final CodeRunner ctxt,
                                                        final Value clob,
                                                        final Value pos,
                                                        final Value length)
        throws Fail.Exception {
        try {
            final Clob c = (Clob) clob.asBlock().asCustom();
            final Block res =
                Block.createString(c.getSubString(pos.asBlock().asInt64(),
                                                  length.asBlock().asInt32()));
            return Value.createFromBlock(res);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_clob_get_sub_string(CodeRunner, Value, Value, Value)'

    /**
     * Returns the length of the passed Clob.
     * @param ctxt context
     * @param clob clob
     * @return the length of the passed Clob
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_clob_length(final CodeRunner ctxt,
                                                final Value clob)
        throws Fail.Exception {
        try {
            final Clob c = (Clob) clob.asBlock().asCustom();
            final Block res = Block.createCustom(Custom.INT_64_SIZE,
                                                 Custom.INT_64_OPS);
            res.setInt64(c.length());
            return Value.createFromBlock(res);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_clob_length(CodeRunner, Value)'

    /**
     * Returns the position of a Clob into another one Clob.
     * @param ctxt context
     * @param clob clob
     * @param clob2 clob to locate
     * @param start starting index of search
     * @return the position of a Clob into another one Clob
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_clob_position_clob(final CodeRunner ctxt,
                                                       final Value clob,
                                                       final Value clob2,
                                                       final Value start)
        throws Fail.Exception {
        try {
            final Clob c = (Clob) clob.asBlock().asCustom();
            final Clob c2 = (Clob) clob2.asBlock().asCustom();
            final Block res = Block.createCustom(Custom.INT_64_SIZE,
                                                 Custom.INT_64_OPS);
            res.setInt64(c.position(c2, start.asBlock().asInt64()));
            return Value.createFromBlock(res);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_clob_position_clob(CodeRunner, Value, Value, Value)'

    /**
     * Returns the position of a string into the passed Clob.
     * @param ctxt context
     * @param clob clob
     * @param bytes string to locate
     * @param start starting index of search
     * @return the position of a passed string into the passed Clob
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_clob_position_string(final CodeRunner ctxt,
                                                         final Value clob,
                                                         final Value bytes,
                                                         final Value start)
        throws Fail.Exception {
        try {
            final Clob c = (Clob) clob.asBlock().asCustom();
            final Block res = Block.createCustom(Custom.INT_64_SIZE,
                                                 Custom.INT_64_OPS);
            res.setInt64(c.position(bytes.asBlock().asString(),
                                    start.asBlock().asInt64()));
            return Value.createFromBlock(res);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_clob_position_string(CodeRunner, Value, Value, Value)'

    /**
     * Returns an ASCII stream allowing to write the value of the passed Clob.
     * @param ctxt context
     * @param clob clob
     * @param pos position where data should be written
     * @return an ASCII stream allowing to write the value of the passed Clob
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_clob_set_ascii_stream(final CodeRunner ctxt,
                                                          final Value clob,
                                                          final Value pos)
        throws Fail.Exception {
        try {
            final Clob c = (Clob) clob.asBlock().asCustom();
            final OutputStream os = c.setAsciiStream(pos.asBlock().asInt64());
            final Channel ch = new Channel(os);
            ctxt.getContext().addChannel(ch);
            final Block res = Block.createCustom(Custom.CHANNEL_SIZE,
                                                 Custom.CHANNEL_OPS);
            res.setCustom(ch);
            return Value.createFromBlock(res);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_clob_set_ascii_stream(CodeRunner, Value, Value)'

    /**
     * Changes the value of the passed Clob.
     * @param ctxt context
     * @param clob clob
     * @param pos position where data should be written
     * @param bytes string to write to Clob
     * @return the number of characters actually written
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_clob_set_string(final CodeRunner ctxt,
                                                    final Value clob,
                                                    final Value pos,
                                                    final Value bytes)
        throws Fail.Exception {
        try {
            final Clob c = (Clob) clob.asBlock().asCustom();
            final Block res = Block.createCustom(Custom.INT_32_SIZE,
                                                 Custom.INT_32_OPS);
            res.setInt32(c.setString(pos.asBlock().asInt64(),
                                     bytes.asBlock().asString()));
            return Value.createFromBlock(res);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_clob_set_string(CodeRunner, Value, Value, Value)'

    /**
     * Changes the value of the passed Clob.
     * @param ctxt context
     * @param clob clob
     * @param pos position where data should be written
     * @param bytes string to write to Clob
     * @param ofs offset (in <i>bytes</i>) of data to write
     * @param len length of data to write
     * @return the number of characters actually written
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_clob_set_string_ofs(final CodeRunner ctxt,
                                                        final Value clob,
                                                        final Value pos,
                                                        final Value bytes,
                                                        final Value ofs,
                                                        final Value len)
        throws Fail.Exception {
        try {
            final Clob c = (Clob) clob.asBlock().asCustom();
            final Block res = Block.createCustom(Custom.INT_32_SIZE,
                                                 Custom.INT_32_OPS);
            res.setInt32(c.setString(pos.asBlock().asInt64(),
                                     bytes.asBlock().asString(),
                                     ofs.asLong(),
                                     len.asLong()));
            return Value.createFromBlock(res);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_clob_set_string_ofs(CodeRunner, Value, Value, Value, Value, Value)'

    /**
     * Truncates the passed Clob.
     * @param ctxt context
     * @param clob clob
     * @param len new Clob length
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_clob_truncate(final CodeRunner ctxt,
                                                  final Value clob,
                                                  final Value len)
        throws Fail.Exception {
        try {
            final Clob c = (Clob) clob.asBlock().asCustom();
            c.truncate(len.asBlock().asInt64());
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_clob_truncate(CodeRunner, Value, Value)'

} // end class 'Clobs'
